<template>
  <div v-show="dialogValue" class="add-form f-c">
    <div class="header">
      <div class="title">
        添加/编辑
      </div>
      <el-button @click="onPageBack">返回</el-button>
    </div>
    <div class="add-form-content f-g">
      <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @close="onPageBack"
        @save="onSave"
      />
    </div>
  </div>
</template>

<script>
import { http } from '@/api'
import dayjs from 'dayjs'
export default {
  name: 'FormAdd',
  data() {
    return {
      dialogValue: false,
      ruleForm: {
      },
      rules: {
        projectName: [{ required: true, message: '不能为空' }]
      },
      formList: [
        {
          key: 'projectName',
          name: 'input',
          label: '项目名称',
          colSpan: 12
        },
        {
          key: 'category',
          name: 'select',
          label: '会议类别',
          options: [
            { value: '1', name: '项目' },
            { value: '2', name: '工程' }
          ],
          colSpan: 12
        },
        {
          key: 'pocket',
          name: 'select',
          label: '资金选择',
          colSpan: 12,
          options: [
            { value: '1', name: '10-20 万' },
            { value: '2', name: '20-30 万' },
            { value: '3', name: '30-40 万' },
            { value: '4', name: '40-50 万' },
            { value: '5', name: '50-100 万' }
          ]
        },
        {
          key: 'projectName',
          name: 'input',
          label: '项目主体',
          colSpan: 12
        },
        {
          key: 'projectContent',
          name: 'input',
          type: 'textarea',
          label: '项目内容',
          colSpan: 24
        }
      ]
    }
  },
  created() {

  },
  methods: {
    getForm() {
      const form = {
        id: undefined
      }
      this.formList.forEach(el => {
        form[el.key] = ''
      })
      return form
    },
    async open(item) {
      this.ruleForm = this.getForm()
      if (item) {
        for (const key in this.ruleForm) {
          this.ruleForm[key] = item[key]
        }
      }
      this.dialogValue = true
    },
    onPageBack() {
      this.$emit('get-page')
      this.dialogValue = false
    },
    onSave() {
      http.project_center[this.ruleForm.id ? 'update' : 'create']({ ...this.ruleForm }).then(res => {
        this.$message.success(res.msg)
        this.onPageBack()
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
